package socketmessage.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import config.Interceptor.PassToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import socketmessage.entity.Socketmessage;
import socketmessage.mapper.SocketmessageMapper;
import socketmessage.service.impl.Socketmessageserviceimpl;
import utils.CommonRetumType;

import java.util.HashMap;

@RestController
@RequestMapping("/messager")
public class Messagecontroller {
    @Autowired
    Socketmessageserviceimpl socketmessageserviceimpl;

    @Autowired
    SocketmessageMapper socketmessageMapper;


    @GetMapping("/read")
    @PassToken
    public CommonRetumType read(@RequestParam("pageNum") int pageNum,
                                @RequestParam("pageSize") int pageSize,
                                @RequestParam("openId") String openid,
                                @RequestParam("lastopenid") String lastopenid)
    {
        HashMap<String,IPage<Socketmessage>> hashMap =new HashMap<>();
        Page<Socketmessage> userPage = new Page<Socketmessage>(pageNum,pageSize);

        String openlast = openid+"#"+lastopenid;
        String oppositeopenid = lastopenid+"#"+openid;
        QueryWrapper<Socketmessage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("usersocketid",openlast).or().eq("usersocketid",oppositeopenid).orderByDesc("createtime");
        IPage<Socketmessage> messageList=socketmessageMapper.selectPage(userPage,queryWrapper);
        hashMap.put("openidmessage",messageList);
        return CommonRetumType.create(hashMap,"信息查询成功");
    }

    @GetMapping("/create")
    @PassToken
    public CommonRetumType create(Socketmessage socketmessage)
    {
        return CommonRetumType.create(socketmessageserviceimpl.insertSelective(socketmessage),"成");
    }

    @GetMapping("/delete")
    @PassToken
    public CommonRetumType delete(@RequestParam("openId") String openid,
                                  @RequestParam("lastopenid") String lastopenid)
    {
        String usermessgeid = openid+"#"+lastopenid;
        String useropenid = lastopenid+"#"+openid;
        socketmessageserviceimpl.deleteByopenid(usermessgeid);

        return CommonRetumType.create(socketmessageserviceimpl.deleteByopenid(useropenid),"删除信息成功");
    }
}
